#ifdef DUMP_MEMORY_LEAK
#include <vld.h>
#endif
#include "serial_surpport.h"
#include "i_package.h"
#include "package_mgr.h"

ostream& IPackage::write(ostream& os, IPackage& packge)
{
	Header header(packge.messageID(), packge.size());
	serial::write(os, header);
	packge.push(os);
	return os;
}

ostream& IPackage::write(ostream& os, IPackage& packge, unsigned int seq)
{
	Header header(packge.messageID(), packge.size());
	header.seq_ = seq;
	serial::write(os, header);
	packge.push(os);
	return os;
}

IPackage::Ptr IPackage::read(istream& is)
{
	Header header;
	serial::read(is, header);
	IPackage::Ptr package = PackageMgr::getInstace()->get(header.id_);
	package->poll(is);	
	return package;
}

IPackage::Ptr IPackage::read(istream& is, const Header& header)
{
	IPackage::Ptr package = PackageMgr::getInstace()->get(header.id_);
	if(package.get() == NULL) { return package; }
	package->poll(is);	
	return package;
}